let express = require("express")();
let mysql = require("mysql");
let expressStatic = require("express-static");
let bodyParser = require("body-parser");
express.use(bodyParser.json());
express.use(bodyParser.urlencoded({ extended: false }));

var sql = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'zhongxing'
});
sql.connect();
express.get("/getlist", (require, response) => {
    sql.query(`SELECT * FROM details`, (error, data) => {
        response.send(data)
    })
})
express.get("/getitem", (request, response) => {
    let id = request.query.id
    sql.query(`SELECT * FROM details WHERE id="${id}"`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            response.send(data[0])
        }
    })
})

express.post("/getuser", (require, response) => {
    let txt = require.body
    sql.query(`SELECT * FROM user WHERE username="${txt.username}" AND password="${txt.password}"`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            if (data.length === 0) {
                response.send("error")
                console.log(error)
            } else {
                response.send(JSON.stringify(data))
            }
        }
    })
})
express.post("/adduser", (request, response) => {
    let txt = request.body
    sql.query(`INSERT INTO user (username,password,name) VALUES ("${txt.username}","${txt.password}","${txt.name}")`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            response.send("success")
        }
    })
})
express.get("/interposition", (request, response) => {
    let txt = request.query
    sql.query(`INSERT INTO orders (goodsid,userid) VALUES ("${txt.id}","${txt.userid}")`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            response.send("success")
        }
    })
})
express.get("/getid", (request, response) => {
    let txt = request.query
    sql.query(`SELECT * FROM details WHERE id="${txt.id}"`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            if (data.length === 0) {
                response.send("error")
            } else {
                response.send(data[0])
            }
        }
    })
})
express.get("/getorder", (request, response) => {
    let txt = request.query
    sql.query(`SELECT orders.userid,orders.goodsid,details.id,details.tittle,details.imgone,details.currentPrice FROM orders,details WHERE orders.userid="${txt.userid}" AND details.id=orders.goodsid`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            if (data.length === 0) {
                response.send("error")
                console.log(error)
            } else {
                response.send(data)
            }
        }
    })
})
express.get("/deleteorder", (request, response) => {
    let txt = request.query
    sql.query(`DELETE FROM orders WHERE goodsid="${txt.id}" `, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            response.send("success")
        }
    })
})
express.get("/addorder", (request, response) => {
    let txt = request.query
    sql.query(`INSERT INTO orders (userid,postAddress,postPhone,postName) VALUES ("${txt.id}","${txt.postAddress}","${txt.postPhone}","${txt.postName}")`, (error, data) => {
        if (error) {
            response.send("error")
            console.log(error)
        } else {
            response.send("success")
        }
    })
})
express.use(expressStatic(__dirname + '/project2'));
express.listen(81)